package com.superboboge.controller;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.superboboge.CommonConfigPeople;
import com.superboboge.User;
import com.superboboge.UserDAO;
import com.superboboge.UserParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class TestController {
    public final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    CommonConfigPeople commonConfigPeople;

    @Resource
    UserDAO userDAO;


    @RequestMapping("/insert")
    public void pagaTest(){
        for (int i = 0; i < 15; i++) {
            User user = new User();
            user.setName("bobo"+i);
            user.setAge(i);
            user.setGmtCreated(new Date());
            user.setGmtModified(new Date());
            userDAO.save(user);
        }
    }
    @RequestMapping("/pageTest")
    public List<User> pageTest(UserParam userParam){
        Specification<User> specification = com.github.wenhao.jpa.Specifications.<User>and()
                .eq(StringUtils.isNotEmpty(userParam.getName()),"name", userParam.getName())
                .ge(null != userParam.getStartDate(),"gmtCreated",userParam.getStartDate())
                .le(null != userParam.getEndDate(),"gmtCreated",userParam.getEndDate())
                .build();
        return userDAO.findAll(specification, new PageRequest(userParam.getPage(), userParam.getPageSize())).getContent();
    }

    @RequestMapping("/testConfig")
    public CommonConfigPeople test(){
        log.debug("测试日志{}",commonConfigPeople.getName());
        return commonConfigPeople;
    }


}
